package com.play.ballen.home.ui.fragment

import androidx.recyclerview.widget.LinearLayoutManager
import com.hjq.toast.ToastUtils
import com.hyphenate.easeui.constants.EaseConstant
import com.play.ballen.R
import com.play.ballen.databinding.FragmentMyBottleBinding
import com.play.ballen.home.adapter.BottleListAdapter
import com.play.ballen.home.ui.BottleDetailActivity
import com.play.ballen.home.vm.PLPFragmentVM
import com.play.ballen.im.PvtChatActivity
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.yuven.baselib.component.BaseMVVMFragment
import com.yuven.baselib.utils.ResourceUtil
import com.yuven.baselib.widget.MultiStateView

class MeBottleFragment: BaseMVVMFragment<PLPFragmentVM, FragmentMyBottleBinding>() {
    override fun layoutId(): Int {
        return R.layout.fragment_my_bottle
    }

    private val adapter: BottleListAdapter by lazy {
        BottleListAdapter(arrayListOf())
    }



    companion object {
        val TAG: String = MeBottleFragment::class.java.simpleName
        fun newInstance(): MeBottleFragment {
            return MeBottleFragment()
        }
    }



    override fun viewModelClass(): Class<PLPFragmentVM> {
        return PLPFragmentVM::class.java
    }

    override fun initView() {
    }


    override fun initObserver() {
        super.initObserver()
        viewModel.pImGroup.observe(this) {
            if (it != null) {
                PvtChatActivity.actionStart(mContext,it.room_id)
            }
        }
        viewModel.bottleList.observe(this) {
            dataBinding.refreshLayout.finishRefresh()
            dataBinding.refreshLayout.finishLoadMore()

            if (!it.isNullOrEmpty()) {
                if (viewModel.page == 1) {
                    adapter.setList(it)
                    dataBinding.msvLayout.viewState = MultiStateView.VIEW_STATE_CONTENT
                } else {
                    adapter.addData(it)
                }
                viewModel.page++
            } else {
                //no data
                if (viewModel.page == 1) {
                    dataBinding.msvLayout.viewState = MultiStateView.VIEW_STATE_EMPTY
                } else {
                    ToastUtils.show(ResourceUtil.getString(R.string.load_end))
                }
            }
        }
    }

    override fun initData() {
        dataBinding.refreshLayout.setEnableLoadMore(true)
        dataBinding.refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                viewModel.page = 1
                viewModel.getBottleList()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                viewModel.getBottleList()
            }
        })

        dataBinding.rv.layoutManager = LinearLayoutManager(mContext)
        dataBinding.rv.adapter = adapter
        adapter.setOnItemClickListener { a, view, position ->
            val info = adapter.data[position].bottle_id
            viewModel.read(info.toString())
            BottleDetailActivity.start(mContext, info)
        }
        dataBinding.msvLayout.viewState = MultiStateView.VIEW_STATE_LOADING
    }

    override fun initIntent() {

    }

    override fun onResume() {
        super.onResume()
        viewModel.page = 1
        viewModel.getBottleList()
    }
}